package com.mall.dao;

import com.mall.domain.Menus;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 菜单的持久层接口
 * @author : Bei-Zhen
 * @date : 2020-12-28 15:19
 */
@Repository
public interface IMenusDao {

    /**
     * 查询菜单父节点
     * @return
     */
    @Results(id = "menusMap",
            value = {
                    @Result(id = true,property = "id",column = "id"),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(column = "id",
                            property = "children",
                            many = @Many(select = "com.mall.dao.IMenusDao.listChild",fetchType = FetchType.LAZY)
                    )
            })
    @Select("select * from tb_menus where parent_id=0 and status=1")
    List<Menus> getTree();

    /**
     * 查询菜单子节点
     * @param parentId
     * @return
     */
    @ResultMap("menusMap")
    @Select("select * from tb_menus where parent_id=#{parentId} and status=1")
    List<Menus> listChild(@Param("parentId")int parentId);
}
